﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DFast.cs" company="">
//   
// </copyright>
// <summary>
//   The d fast.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using Systemathics.FrameWork;

namespace Systemathics.Indicators
{

    [Serializable]
    public sealed class DFast : Indicator
    {
        private readonly KFast KFast;
        private readonly Queue<Decimal> queue;
        private Decimal mu, a, b;

        public int Length { get; private set; }

        public DFast(Instrument instr, Data dt, BarType bt, int value,int length): base(instr ,dt ,  bt, value)
        {
            Length = length;
            KFast = new KFast(instr,dt, bt, value, length);
            a = new decimal((Length - 1) / (double)Length);
            b = new decimal(1 / (double)Length);
        }

        protected override void Calculate(object price)
        {
            if (queue.Count < Length)
            {
                queue.Enqueue(KFast.Last());
                mu = queue.Average();
            }
            else
            {
                mu = a * mu + b * KFast.Last();
                Add(Time.Now, mu);
                IndicatorCalculated(this);
            }

        }
    }
}